package com.hongxun.budget.ui.fragment.tk;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DOtherCostEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.greenentity.tk.BOtherQuantitiesEntity;
import com.hongxun.greenentity.tk.DOtherCostEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import butterknife.OnClick;

/**
 * 工程量
 */

public class QuantitiesFragment extends BaseListFragment {

    List<DOtherCostEntity> datas = new ArrayList<>();
    List<DOtherCostEntity> deleteDatas = new ArrayList<>();
    SuperAdapter<DOtherCostEntity> adapter;


    @Override
    protected int getMyListLayoutId() {
        return R.layout.fragment_quantities;
    }

    @Override
    public void initView() {
        super.initView();
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
        initData();
    }

    private void initData() {
        datas.addAll(DBUtils.getDOtherCostEntities(context));
        adapter.notifyDataSetChanged();
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<DOtherCostEntity>(R.layout.content_fragment_quantities) {
            @Override
            public void bindView(ViewHolder viewHolder, final DOtherCostEntity d, final int i) {
                ((TextView) viewHolder.getView(R.id.device_type)).setText(d.getOCSubclass()
                        + "   (" + DBUtils.getBDataDictionaryEntity("work_type", d.getOCWork()).getDValue() + ")");
                ((TextView) viewHolder.getView(R.id.name)).setText(d.getOCName());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(d.getOCParameter());
                ((TextView) viewHolder.getView(R.id.count)).setText(d.getOQCount() + "");
                ((TextView) viewHolder.getView(R.id.price)).setText(d.getOCPriceall() + "元(" + d.getOCPrice() + "*" + d.getOQCount() + ")");
                ((TextView) viewHolder.getView(R.id.unit)).setText("( " + d.getOCMeterUnit() + " )");
                viewHolder.getView(R.id.delete_old_device).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        datas.remove(d);
                        adapter.notifyDataSetChanged();
                        deleteDatas.add(d);
                    }
                });
            }
        };
    }


    public void insertDB() {
        for (DOtherCostEntity d : deleteDatas) {
            d.setCCDatastate("删除");
        }
        GreenDaoManager.getInstance().getmDaoSession().getDOtherCostEntityDao().insertOrReplaceInTx(datas);
        GreenDaoManager.getInstance().getmDaoSession().getDOtherCostEntityDao().updateInTx(deleteDatas);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            if (requestCode == 1) {
                BOtherQuantitiesEntity b = (BOtherQuantitiesEntity) data.getSerializableExtra("quantity");
                setD(b);
            } else if (requestCode == 4) {
                DPointEntity dPointEntity = (DPointEntity) data.getSerializableExtra("point");
                copyPoint(dPointEntity);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void copyPoint(DPointEntity d) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DOtherCostEntity> list = session.getDOtherCostEntityDao().queryBuilder()
                .where(DOtherCostEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))
                        , DOtherCostEntityDao.Properties.PPGuid.eq(d.getGuid())
                        , DOtherCostEntityDao.Properties.CCDatastate.notEq("删除")).list();
        for (DOtherCostEntity o : list) {
            DOtherCostEntity dd = new DOtherCostEntity();
            dd.setGuid(UUID.randomUUID().toString());
            dd.setPGuid((String) SPUtils.get(context, "project_guid", ""));
            dd.setPPGuid((String) SPUtils.get(context, "UUID", ""));
            dd.setOCWork(o.getOCWork());
            dd.setOCSubclass(o.getOCSubclass());
            dd.setOCName(o.getOCName());
            dd.setOCParametertype(o.getOCParametertype());
            dd.setOCParameter(o.getOCParameter());
            dd.setOCMeterUnit(o.getOCMeterUnit());
            dd.setOCPrice(o.getOCPrice());
            dd.setOCPriceall(o.getOCPriceall());
            dd.setOQCount(o.getOQCount());
            dd.setOCRemarks(o.getOCRemarks());
            dd.setCCDatastate("新增");
            datas.add(dd);
        }
        adapter.notifyDataSetChanged();
    }

    private void setD(BOtherQuantitiesEntity b) {
        datas.add(DBUtils.addOtherCost(context, b));
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.add_button)
    public void onViewClicked() {
        DBUtils.addOtherQuantity(this, 1);
    }
}
